<?php
/**
 * 规格管理
 * Created by PhpStorm.
 * User: qinzhi
 * Date: 2017/07/03
 * Time: 20:19
 */
class Controller_Adminmall_Spec extends Controller_Adminmall_Abstract
{

    public function indexAction(){
        $where = array();
        $name = FRequest::getRequestString('name');
        if($name > 0){
            $where['name'] = array('like'=>"%{$name}%");
        }
        $specT = new FTable('spec','','xhs_slave');
        $page = FRequest::getRequestInt('page');
        $limit = 20;
        $dataList = $specT->where($where)->page($page)->limit($limit)->select();

        $pagerInfo = $specT->getPagerInfo();
        $this->assign('page_info', $pagerInfo);
        $this->assign('dataList',$dataList);
        $this->display('goods/spec');
    }

    public function addAction(){
        if($this->isPost()){
            $name = FRequest::getRequestString('name');
            if(empty($name)){
                FResponse::output(array('code'=>0,'msg'=>'规格名称不能为空'));
            }

            $value = !empty($_POST['value'])?$_POST['value']:'';
            $value = empty($value) ?: json_encode($value, JSON_UNESCAPED_UNICODE);
            $spec = [
                'name' => $name,
                'value' => $value,
                'remark' => FRequest::getRequestString('remark')
            ];

            $specT = new FTable('spec','','xhs_master');
            if($id = $specT->insert($spec)){
                $spec['id'] = $id;
                FResponse::output(array('code'=>1,'msg'=>'添加成功','data'=> $spec));
            }else{
                FResponse::output(array('code'=>0,'msg'=>'添加失败'));
            }
        }
    }

    public function editAction(){
        $id = FRequest::getRequestInt('id');
        $name = FRequest::getRequestString('name');
        if(empty($name)){
            FResponse::output(array('code'=>0,'msg'=>'规格名称不能为空'));
        }

        $value = !empty($_POST['value'])?$_POST['value']:'';
        $value = empty($value) ?: json_encode($value, JSON_UNESCAPED_UNICODE);
        $spec = [
            'name' => $name,
            'value' => $value,
            'remark' => FRequest::getRequestString('remark')
        ];

        $specT = new FTable('spec','','xhs_master');

        if($specT->where(array('id'=>$id))->update1($spec)){
            FResponse::output(array('code'=>1,'msg'=>'更新成功'));
        }else{
            FResponse::output(array('code'=>0,'msg'=>'更新失败'));
        }
    }

    public function getAction(){
        if($this->isPost()){
            $id = FRequest::getRequestInt('id');
            $specT = new FTable('spec','','xhs_slave');
            $spec = $specT->where('id='.$id)->find();
            FResponse::output($spec);
        }
    }
}